package com.dgut.shopManage.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dgut.shopManage.dao.ShopRepository;
import com.dgut.shopManage.entity.Shop;
import com.dgut.shopManage.entity.dto.ShopDTO;


@Service
@Transactional
public class ShopService implements IShopService {

	@Autowired
	private ShopRepository shopRepository;

	public void saveOrUpdate(Shop shop) {	
		shop.setMerchant(shop.getMerchant());
		shopRepository.save(shop);
	}
	public void delete(Shop shop) {
		shopRepository.delete(shop);
	}
	@Transactional(readOnly = true)
	public Shop findOne(Long id) {
		return shopRepository.findOne(id);
	}
	@Transactional(readOnly = true)
	public List<Shop> findAll() {
		return (List<Shop>) shopRepository.findAll();
	}

	/*@Transactional(readOnly = true)
	public Page<Shop> findAll(Specification<Shop> spec, Pageable pageable) {
		return shopRepository.findAll(spec, pageable);
	}*/
	@Override
	public Page<ShopDTO> findAll(Specification<Shop> spec, Pageable pageable) {
		Page<Shop> list = shopRepository.findAll(spec,pageable);
		List<ShopDTO> dtoLists = new ArrayList<ShopDTO>();
		for(Shop entity : list.getContent()) {
			ShopDTO dto = new ShopDTO();
			ShopDTO.entityToDto(entity, dto);
			dtoLists.add(dto);
		}
		return new PageImpl<ShopDTO>(dtoLists,pageable,list.getTotalElements());
	}
	
}
